package com.atguigu.tingshu.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderDerate;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.context.OrderHandlerInterceptorContext;
import com.atguigu.tingshu.order.context.OrderTradeStrategyContext;
import com.atguigu.tingshu.order.enumeration.OrderStatusEnum;
import com.atguigu.tingshu.order.enumeration.PayWayEnum;
import com.atguigu.tingshu.order.mapper.OrderDerateMapper;
import com.atguigu.tingshu.order.mapper.OrderDetailMapper;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderDerateMapper orderDerateMapper;
    @Autowired
    private KafkaService kafkaService;


    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {
//        OrderInfoVo orderInfoVo = new OrderInfoVo();
//        String itemType = tradeVo.getItemType();
//        if (SystemConstant.ALBUM_PAY_TYPE_FREE.equals(itemType)){
//            //专辑购买
//
//
//        }   else if(SystemConstant.ORDER_ITEM_TYPE_TRACK.equals(itemType)) {
//
//            // 声音购买
//
//        }else if(SystemConstant.ORDER_ITEM_TYPE_VIP.equals(itemType)) {
//
//            // 购买vip
//
//        }
        OrderTradeStrategy orderTradeStrategy = OrderTradeStrategyContext.getOrderTradeStrategy(tradeVo.getItemType());
        OrderInfoVo orderInfoVo = orderTradeStrategy.trade(tradeVo);
        return orderInfoVo;

    }

    @Override
    public String submitOrder(OrderInfoVo orderInfoVo) {
        OrderInfoVo orderInfo = OrderHandlerInterceptorContext.entry(orderInfoVo);

        // 返回订单编号
        return orderInfo.getTradeNo();
    }

    @Override
    public void orderCancel(String orderNo) {
        LambdaQueryWrapper<OrderInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderInfo::getOrderNo, orderNo);
        OrderInfo orderInfo = orderInfoMapper.selectOne(lambdaQueryWrapper);
        //判断订单状态
        if (SystemConstant.ORDER_STATUS_UNPAID.equals(orderInfo.getOrderStatus())){
            //把订单转态改为已取消
            orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_CANCEL);


            // 在执行真实的数据库修改操作的时候，再次进行判断，判断当前订单的状态是否为未支付，如果是在执行真实的修改操作。
            // update order_info set order_status = '0903' where order_no = ? and order_status = '0901'
            lambdaQueryWrapper.eq(OrderInfo::getOrderStatus, SystemConstant.ORDER_STATUS_UNPAID);
            orderInfoMapper.update(orderInfo , lambdaQueryWrapper) ;


        }

    }

    @Override
    public void orderPaySuccess(String orderNo) {
        //  根据orderNo修改订单状态数据
        OrderInfo orderInfo = this.getOrderInfoByOrderNo(orderNo, SystemConstant.ORDER_STATUS_PAID);

        orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_PAID);     // 把订单的状态改为已支付
        orderInfoMapper.updateById(orderInfo) ;
        log.info("将订单的状态改为已支付...orderNo: {}" , orderNo);

        // 向Kafka中发送保存交易信息的消息, 消息的内容: orderNo , userId , itemIdList , itemType
        UserPaidRecordVo userPaidRecordVo = new UserPaidRecordVo() ;
        userPaidRecordVo.setOrderNo(orderNo);
        userPaidRecordVo.setUserId(orderInfo.getUserId());
        userPaidRecordVo.setItemType(orderInfo.getItemType());
        List<Long> itemIdList = orderInfo.getOrderDetailList().stream().map(orderDetail -> orderDetail.getItemId()).collect(Collectors.toList());
        userPaidRecordVo.setItemIdList(itemIdList);
        kafkaService.sendMessage(KafkaConstant.QUEUE_USER_PAY_RECORD , JSON.toJSONString(userPaidRecordVo));
        log.info("发送保存交易信息消息成功...infoContent: {}" , JSON.toJSONString(userPaidRecordVo));

    }

    @Override
    public OrderInfo getOrderInfoByOrderNo(String orderNo) {
// 创建LambdaQueryWrapper对象，封装查询条件
        LambdaQueryWrapper<OrderInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderInfo::getOrderNo , orderNo) ;
        OrderInfo orderInfo = orderInfoMapper.selectOne(lambdaQueryWrapper);

        // 查询订单所对应的明细数据
        LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>() ;
        orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId , orderInfo.getId()) ;
        List<OrderDetail> orderDetailList = orderDetailMapper.selectList(orderDetailLambdaQueryWrapper);
        orderInfo.setOrderDetailList(orderDetailList);

        // 查询订单的减免明细数据
        LambdaQueryWrapper<OrderDerate> derateLambdaQueryWrapper = new LambdaQueryWrapper<>() ;
        derateLambdaQueryWrapper.eq(OrderDerate::getOrderId , orderInfo.getId()) ;
        List<OrderDerate> orderDerateList = orderDerateMapper.selectList(derateLambdaQueryWrapper);
        orderInfo.setOrderDerateList(orderDerateList) ;

        // 设置状态的名称
        orderInfo.setOrderStatusName(OrderStatusEnum.getOrderStatusName(orderInfo.getOrderStatus()));

        // 设置支付方式的名称
        orderInfo.setPayWayName(PayWayEnum.getPayWayName(orderInfo.getPayWay()));

        // 返回
        return orderInfo;
    }

    @Override
    public IPage<OrderInfo> findOrderInfoByPage(Integer pageNo, Integer pageSize) {
        // 获取当前登录用户的id
        Long userId = AuthContextHolder.getUserId();
        LambdaQueryWrapper<OrderInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderInfo::getUserId , userId);

        // 创建Page对象
        Page<OrderInfo> page = new Page<>(pageNo ,pageSize) ;
        this.page(page , lambdaQueryWrapper) ;

        page.getRecords().stream().forEach(orderInfo -> {

            Long orderId = orderInfo.getId();
            LambdaQueryWrapper<OrderDetail> detailLambdaQueryWrapper = new LambdaQueryWrapper<>() ;
            detailLambdaQueryWrapper.eq(OrderDetail::getOrderId , orderId);
            List<OrderDetail> orderDetailList = orderDetailMapper.selectList(detailLambdaQueryWrapper);
            orderInfo.setOrderDetailList(orderDetailList) ;

            orderInfo.setOrderStatusName(OrderStatusEnum.getOrderStatusName(orderInfo.getOrderStatus()));
            orderInfo.setPayWayName(PayWayEnum.getPayWayName(orderInfo.getPayWay()));

        });

        // 返回
        return page;
    }

    private OrderInfo getOrderInfoByOrderNo(String orderNo, String orderStatusPaid) {

        // 创建LambdaQueryWrapper对象，封装查询条件
        LambdaQueryWrapper<OrderInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderInfo::getOrderNo, orderNo);
        OrderInfo orderInfo = orderInfoMapper.selectOne(lambdaQueryWrapper);

        // 查询订单所对应的明细数据
        LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, orderInfo.getId());
        List<OrderDetail> orderDetailList  = orderDetailMapper.selectList(orderDetailLambdaQueryWrapper);
        orderInfo.setOrderDetailList(orderDetailList);

        // 查询订单的减免明细数据
        LambdaQueryWrapper<OrderDerate> derateLambdaQueryWrapper = new LambdaQueryWrapper<>() ;
        derateLambdaQueryWrapper.eq(OrderDerate::getOrderId , orderInfo.getId()) ;
        List<OrderDerate> orderDerateList = orderDerateMapper.selectList(derateLambdaQueryWrapper);
        orderInfo.setOrderDerateList(orderDerateList) ;


        // 设置状态的名称
        orderInfo.setOrderStatusName(OrderStatusEnum.getOrderStatusName(orderInfo.getOrderStatus()));

        // 设置支付方式的名称
        orderInfo.setPayWayName(PayWayEnum.getPayWayName(orderInfo.getPayWay()));

        // 返回
        return orderInfo;
    }

}
